﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TD.Interface.Credits
{
    internal abstract class Effect
    {
        private TimeSpan elapsedTime;

        private Matrix matrix;
        public Matrix Matrix { get { return this.matrix; } }

        /// <summary>
        /// The delay in miliseconds.
        /// </summary>
        private double milisecondsDelay;
        public double MilisecondsDelay { get { return this.milisecondsDelay; } }

        public Effect(int milisecondsDelay)
        { 
            this.matrix = Matrix.Identity;
            this.milisecondsDelay = milisecondsDelay;
            ResetElapsedTime();
        }

        public void Update(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime.TotalMilliseconds >= milisecondsDelay)
            {
                ResetElapsedTime();
                TriggerEfx();

                Matrix[] matrices = ApplyMatrices();
                if (matrices.Length > 0)
                {
                    matrix = matrices[0];
                    for (int i = 1; i < matrices.Length; i++)
                    {
                        matrix *= matrices[i];
                    }
                }
            }
        }

        private void ResetElapsedTime()
        {
            elapsedTime = TimeSpan.Zero;
        }

        protected virtual void TriggerEfx()
        {
            // Specific effects can use the trigger to execute their effect.
        }
        protected abstract Matrix[] ApplyMatrices();

        public event OnDone Done;
        protected virtual void Overtime()
        {
            if (Done != null)
            {
                Done();
            }
        }

        public virtual void Reset()
        {
            // Do the reset.
        }
    }

    internal delegate void OnDone();
}
